package org.therandoms.textgen.cli;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

import javax.xml.parsers.DocumentBuilderFactory;

import org.therandoms.textgen.TextGeneratorDictionary;
import org.therandoms.textgen.TextGeneratorDictionaryXMLWorker;
import org.therandoms.textgen.WordnikOptions;
import org.therandoms.textgen.WordnikOptions.WordnikSortBy;
import org.therandoms.textgen.WordnikOptions.WordnikSortOrder;
import org.therandoms.textgen.WordnikPuller;
import org.w3c.dom.Document;

/**
 * A simple command line tester for the random text generation
 * @author Logan Widick
 *
 */
public class TextGenCLI {

	/**
	 * The main function. 
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length < 2) {
			System.err.println("Usage: java " + TextGenCLI.class.toString()
					+ " filename " + " text to produce stuff with");
			System.exit(-1);
		}

		File mFile = new File(args[0]);
		try {
			//load grammar
			InputStream is = new FileInputStream(mFile);
			Document mDoc = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder().parse(is);
			TextGeneratorDictionary<String> mDict = TextGeneratorDictionaryXMLWorker
					.createFromXMLDocument(mDoc);
			
			//begin loading random words
			WordnikOptions mOpts = new WordnikOptions(mDict, WordnikSortOrder.ASC, WordnikSortBy.ALPHA);
			
			mOpts.grammarCategoryToLoad = "noun";
			mOpts.wordnikCategoriesToExclude.clear();
			mOpts.wordnikCategoriesToInclude.clear();
			mOpts.wordnikCategoriesToInclude.add("noun");
			WordnikPuller.addRandomWordnikResults(mOpts);
			
			mOpts.grammarCategoryToLoad = "verb";
			mOpts.wordnikCategoriesToExclude.clear();
			mOpts.wordnikCategoriesToInclude.clear();
			mOpts.wordnikCategoriesToInclude.add("verb");
			WordnikPuller.addRandomWordnikResults(mOpts);

			mOpts.grammarCategoryToLoad = "adjective";
			mOpts.wordnikCategoriesToExclude.clear();
			mOpts.wordnikCategoriesToInclude.clear();
			mOpts.wordnikCategoriesToInclude.add("adjective");
			WordnikPuller.addRandomWordnikResults(mOpts);

			mOpts.grammarCategoryToLoad = "pronoun";
			mOpts.wordnikCategoriesToExclude.clear();
			mOpts.wordnikCategoriesToInclude.clear();
			mOpts.wordnikCategoriesToInclude.add("pronoun");
			WordnikPuller.addRandomWordnikResults(mOpts);

			mOpts.grammarCategoryToLoad = "proper_noun";
			mOpts.wordnikCategoriesToExclude.clear();
			mOpts.wordnikCategoriesToInclude.clear();
			mOpts.wordnikCategoriesToInclude.add("proper-noun");
			WordnikPuller.addRandomWordnikResults(mOpts);

			mOpts.grammarCategoryToLoad = "definite_article";
			mOpts.wordnikCategoriesToExclude.clear();
			mOpts.wordnikCategoriesToInclude.clear();
			mOpts.wordnikCategoriesToInclude.add("definite-article");
			WordnikPuller.addRandomWordnikResults(mOpts);

			mOpts.grammarCategoryToLoad = "preposition";
			mOpts.wordnikCategoriesToExclude.clear();
			mOpts.wordnikCategoriesToInclude.clear();
			mOpts.wordnikCategoriesToInclude.add("preposition");
			WordnikPuller.addRandomWordnikResults(mOpts);

			//perform bad word tests
			System.out.println("Bad word test (chicken): " + WordnikPuller.isBadWord("chicken"));
			System.out.println("Bad word test (shit): " + WordnikPuller.isBadWord("shit"));

			//get the production categories to start at
			ArrayList<String> wordsList = new ArrayList<String>(
					Arrays.asList(args));
			wordsList.remove(0); //remove the name of the grammar file

			//Make random nonsense
			StringBuffer mBuf = new StringBuffer();
			
			//Buffer for the randomly made text
			final LinkedList<String> outList = new LinkedList<String>();
			
			//make the text
			mDict.generateText(wordsList, outList);
			
			final StringBuffer outBuf = new StringBuffer();
			for(String word : outList){
				outBuf.append(word);
				outBuf.append(" ");
			}
			
			System.out.println("Generated text: " + mBuf.toString());
		} catch (Exception e) {
			System.err.println("Error opening file: " + args[0]);
			e.printStackTrace();
			System.exit(-2);
		}

	}

}
